<?php

namespace App\Kuafu\Dashboard\Controller\Merchant;

use App\Kuafu\Base\Controller\BaseController;
use App\Kuafu\Order\Enum\OrderCodeEnum;
use App\Kuafu\Order\Enum\OrderMerchantEnum;
use App\Kuafu\Order\Service\OrderMerchantUseService;
use App\Kuafu\Order\Validate\OrderMerchantValidate;
use Illuminate\Http\Request;

class IndexController extends BaseController
{
    public function __construct(Request $request)
    {
        parent::__construct($request);
    }

    // 商家APP首页数据
    public function index(Request $request)
    {
        $shopId         = (int)$request->header('shopId');
        $showTimeTab    = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt    = (int)$request->get('showStartAt', 0);
        $showEndAt      = (int)$request->get('showEndAt', 0);
        $compareTimeTab = (int)$request->get('compareTimeTab');
        $compareStartAt = (int)$request->get('compareStartAt', 0);
        $compareEndAt   = (int)$request->get('compareEndAt', 0);
        $vipIn          = (int)(bool)$request->get('vipIn', 0); // 会员收入是否计入
        $kmsShopId      = (int)$request->get('kmsShopId', 0);
        if ($kmsShopId) {// 从运营APP查询数据
            $shopId = $kmsShopId;
        }

        $validate = OrderMerchantValidate::index($request);
        if ($validate) {
            return $this->fail(OrderCodeEnum::ERR_ORDER_PARAM_VALID, $validate);
        }

        $data = (new OrderMerchantUseService([$shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->setCompareTimeTab($compareTimeTab, $compareStartAt, $compareEndAt)
            ->indexData($vipIn);

        return $this->success($data);
    }

    // 商家APP首页折线图
    public function indexCharts(Request $request)
    {
        $shopId         = (int)$request->header('shopId');
        $showTimeTab    = (int)$request->get('showTimeTab', OrderMerchantEnum::SHOW_TIME_TAB_TODAY);
        $showStartAt    = (int)$request->get('showStartAt', 0);
        $showEndAt      = (int)$request->get('showEndAt', 0);
        $compareTimeTab = (int)$request->get('compareTimeTab');
        $compareStartAt = (int)$request->get('compareStartAt', 0);
        $compareEndAt   = (int)$request->get('compareEndAt', 0);
        $statisticsType = (int)$request->get('statisticsType', OrderMerchantEnum::STAT_ORIGINAL_PRICE);
        $vipIn          = (int)(bool)$request->get('vipIn', 0); // 会员收入是否计入

        $validate = OrderMerchantValidate::indexCharts($request);
        if ($validate) {
            return $this->fail(OrderCodeEnum::ERR_ORDER_PARAM_VALID, $validate);
        }

        $data = (new OrderMerchantUseService([$shopId]))
            ->setShowTimeTab($showTimeTab, $showStartAt, $showEndAt)
            ->setCompareTimeTab($compareTimeTab, $compareStartAt, $compareEndAt)
            ->orderCharts($statisticsType, OrderMerchantEnum::ORDER_TYPE_TOTAL, 0, $vipIn);

        return $this->success($data);
    }
}
